//
//  UIView+Shadow.m
//  QNReuseTool
//
//  Created by rsh on 2019/10/15.
//  Copyright © 2019年 rsh. All rights reserved.
//

#import "UIView+Shadow.h"
#import "CALayer+Shadow.h"
#import <objc/runtime.h>

static NSString *shadowLayerKey = @"UIViewShadowLayerKey";

@implementation UIView (Shadow)

-(void)addShadow{
    self.layer.shadowColor = [UIColor blackColor].CGColor;
    self.layer.shadowOpacity = 0.25;
    self.layer.shadowRadius = 5;//
}

-(void)removeShadow{
    self.layer.shadowColor = nil;
    self.layer.shadowOpacity = 0.0;
    self.layer.shadowRadius = 0.0;//
}

-(void)addShadowToEdge:(UIRectEdge)edge{
    UIView *shadowView = [[UIView alloc]init];
    if (![self.backgroundColor isEqual:[UIColor clearColor]]) {
        shadowView.backgroundColor = self.backgroundColor;
    }else{
        shadowView.backgroundColor = [UIColor whiteColor];
    }
    [self addSubview:shadowView];
    [self sendSubviewToBack:shadowView];
    [shadowView addShadow];
    
}

-(void)setShadowLayer:(CALayer *)shadowLayer{
    objc_setAssociatedObject(self, &shadowLayerKey, shadowLayer, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

-(CALayer *)shadowLayer{
    
    id layer = objc_getAssociatedObject(self, &shadowLayerKey);
    if (!layer) {
        CALayer *llayer = [[CALayer alloc]init];
        [self.superview.layer addSublayer:llayer];
        llayer.backgroundColor = self.backgroundColor.CGColor;
        llayer.cornerRadius = self.layer.cornerRadius;
        llayer.frame = self.frame;
        [self setShadowLayer:llayer];
        layer = llayer;
    }
    return layer;
}

-(void)addShadowToClippedViewToEdge:(UIRectEdge)edge{
    
    if (!self.superview) {
        return;
    }
    
    CALayer *layer = [self shadowLayer];
    if (!layer) {
        return;
    }
    if ([self.backgroundColor isEqual: [UIColor clearColor]]) {
        layer.backgroundColor = [UIColor whiteColor].CGColor;
    }else{
        layer.backgroundColor = self.backgroundColor.CGColor;
    }
    layer.cornerRadius = self.layer.cornerRadius;
    layer.frame = self.frame;
    [layer addDefaultShadowToEdge:edge];
    
}

-(void)addShadowToClippedViewToEdge:(UIRectEdge)edge
                              color:(nonnull CGColorRef)cColor
                            opacity:(CGFloat)opacity
                       shadowRadius:(CGFloat)radius
                       shadowOffset:(CGSize)sizeOffset{
    
    [self addShadowToClippedViewToEdge:edge
                                 color:cColor
                               opacity:0.25
                          shadowRadius:5
                          shadowOffset:CGSizeZero];
}

/**
 addShadowToEdge
 
 @param edge edge descriptionedge
 @param cColor cColor
 */
-(void)addShadowToClippedViewToEdge:(UIRectEdge)edge
                              color:(nonnull CGColorRef)cColor{
    
    [self addShadowToClippedViewToEdge:edge color:cColor opacity:0.25 shadowRadius:5 shadowOffset:CGSizeZero];
    
}


/**
 addShadowToEdge
 
 @param edge edge
 @param opacity opacity
 */
-(void)addShadowToClippedViewToEdge:(UIRectEdge)edge
                            opacity:(CGFloat)opacity{
    
    [self addShadowToClippedViewToEdge:edge
                                 color:[self backgroundColor].CGColor
                               opacity:opacity
                          shadowRadius:5
                          shadowOffset:CGSizeZero];
    
}


/**
 addShadowToEdge
 
 @param edge edge
 @param radius radius
 */
-(void)addShadowToClippedViewToEdge:(UIRectEdge)edge
                       shadowRadius:(CGFloat)radius{
    
    [self addShadowToClippedViewToEdge:edge
                                 color:[self backgroundColor].CGColor
                               opacity:0.25
                          shadowRadius:radius
                          shadowOffset:CGSizeZero];
    
}


/**
 addShadowToEdge
 
 @param edge edge
 @param sizeOffset sizeOffset
 */
-(void)addShadowToClippedViewToEdge:(UIRectEdge)edge
                       shadowOffset:(CGSize)sizeOffset{
    
    [self addShadowToClippedViewToEdge:edge
                                 color:[self backgroundColor].CGColor
                               opacity:0.25
                          shadowRadius:5
                          shadowOffset:sizeOffset];
    
}



@end
